package com.chhuang.sync;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.chhuang.bean.QueryTermBean;
import com.chhuang.common.Assert;
import com.chhuang.common.DataOperateAbstract;
import com.chhuang.common.ThreadPoolShutdownUtil;

/**
 * 同步删除线程
 * @author HuangChen
 *
 */
class DeleteSyncThread extends DataOperateAbstract implements Runnable {
	private static final Log LOG = LogFactory.getLog(DeleteSyncThread.class);
	
	private Connection conn = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
	private String fromTable = null;
	private String toTable = null;
	private String[] idColumns = null;
	
	private String afterWhere = null;
	
	public DeleteSyncThread(Connection conn, PreparedStatement ps, ResultSet rs, 
			String fromTable, String toTable, String... idColumns){
		this.conn = conn;
		this.ps = ps;
		this.rs = rs;
		this.fromTable = fromTable;
		this.toTable = toTable;
		this.idColumns = idColumns;
	}
	
	/**
	 * 同步删除时用线程池
	 */
	private ExecutorService pool = null;
	
	@Override
	public void run() {
		
		LOG.info("-----table ("+fromTable+"->"+toTable+") start sync delete data-----");
		
		try {
			if(rs.next()){
				pool = Executors.newFixedThreadPool(5);
				StringBuilder afterWhereStrb = new StringBuilder();
				for(int i=0;i<idColumns.length;i++){
					afterWhereStrb.append(idColumns[i]).append(EQUAL).append(QUEST).append(AND);
				}
				int awlen = afterWhereStrb.length();
				afterWhereStrb.delete(awlen-AND_LEN, awlen);
				afterWhere = afterWhereStrb.toString();
				do{
					Object[] args = new Object[idColumns.length];
					for(int i=0;i<idColumns.length;i++){
						args[i] = rs.getObject(idColumns[i]);
					}
					//执行同步删除一条记录
					pool.execute(new DeleteThread(args));
				}while(rs.next());
				ThreadPoolShutdownUtil.shutdownPool(pool, 20, TimeUnit.HOURS);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			c3p0Helper.closes(conn, ps, rs);
		}
		
		LOG.info("-----table ("+fromTable+"->"+toTable+") sync delete data completed-----");
	}
	
	/**
	 * 同步删除一条记录的线程
	 * @author PrinceChen
	 *
	 */
	class DeleteThread extends DataOperateAbstract implements Runnable {
		
		private Object[] args = null;
		
		public DeleteThread(Object... args){
			this.args = args;
		}
		
		@Override
		public void run() {
			if(Assert.isInvalidArray(args)){
				return ;
			}
			QueryTermBean queryTermBean = new QueryTermBean(afterWhere, args);
			
			Connection fromConn = c3p0Helper.getConnection(c3p0Helper.getDataBaseName(0));
			
			//查询来源表是否存在这条记录
			PreparedStatement ps = this.find(fromConn, fromTable, queryTermBean, COUNT);
			ResultSet rs = this.getResultSet(ps);
			try {
				if(rs.next() && rs.getInt(1)==0){
					//来源表中没有这表记录，可以执行删除目的表中的此记录
					Connection toConn = c3p0Helper.getConnection(c3p0Helper.getDataBaseName(1));
					this.delete(toConn, toTable, queryTermBean);
					c3p0Helper.closes(toConn);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				c3p0Helper.closes(fromConn, ps, rs);
			}
		}
		
	}
}